<template>
   <div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch, computed } from 'vue';
  // import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '100px',
    },
    seriesVal: {
      type: Object,
      default: () => {},
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const percentage = computed(() => {
      if (props.seriesVal.data && props.seriesVal.data.length > 0) {
      const value = props.seriesVal.data[0].value;
      const value2 = props.seriesVal.total;
      const percentage =  parseFloat(value / value2) * 100;
      return percentage.toFixed(2); // 保留两位小数
    }
    return 0;
  });
  const colorList = computed(() => {
    if(percentage.value <= 80 ) {
      return ['#19be6b', '#f4f4f5']; // 示例颜色列表，您可以根据需要自定义
    }else if( percentage.value > 80 && percentage.value < 90){
      return ['#ff9900', '#f4f4f5']; // 示例颜色列表，您可以根据需要自定义
    }else{
      return ['#fa3534', '#f4f4f5']; // 示例颜色列表，您可以根据需要自定义
    }

  });

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      // const percentage =  props.seriesVal.data[0].value || 0
      setOptions({
        tooltip: {
          trigger: 'item',
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius:  ['73%', '90%'],
            center: ['50%', '50%'],
            color: colorList.value,
            data: [],
            label: {
              //将视觉引导图关闭
              show: false,
            },
           
            // roseType: 'radius',
            animationType: 'scale',
            animationEasing: 'exponentialInOut',
            animationDelay: function () {
              return Math.random() * 400;
            },
            ...props.seriesVal,
          },
        ],
        graphic: {
            type: 'text',
            left: 'center',
            top: 'center',
            style: {
              // `${percentage}%`
                text: `${percentage.value}%`,
                fill: colorList.value[0],
                fontSize: 14
            }
        },
      });
    },
    { immediate: true },
  );
</script>
